package com.gw.youTubeAudioAPI
{
	import com.gw.youTubeAudioAPI.decode.FLVtoRaw;
	import com.gw.youTubeAudioAPI.phpTube.*;
	import com.gw.youTubeAudioAPI.audioStream.*;
	import flash.utils.ByteArray;
	/**
	 * 	provides the interface to the various tools in the package allowing the client to request an audio stream
	 * 	from a youtube url and then control it as an audio stream
	 *	this is a Grant Skinner singleton implementaion:
	 * 	http://www.gskinner.com/blog/archives/2006/07/as3_singletons.html
	 **/
	
	public class YouTubeAudioAPI 
	{	
		private static var instance:YouTubeAudioAPI;
		
		public static function getInstance():YouTubeAudioAPI 
		{
			if (instance == null) {
				instance = new YouTubeAudioAPI(new SingletonBlocker());
			}
			return instance;
		}
		private var decoder:FLVtoRaw;
		
		public function YouTubeAudioAPI(p_key:SingletonBlocker):void {
			// this shouldn't be necessary unless they fake out the compiler:
			if (p_key == null) {
				throw new Error("Error: Instantiation failed: Use YouTubeAudioAPI.getInstance() instead of new.");
			}
		}
		public function startStream(vidURL:String):void {
			trace("initialising stream...");
			var ripFLV:PHPTube = new PHPTube();
			ripFLV.addEventListener(PHPTubeEvent.FLV_DOWNLOADED, onFLVDownload);
			ripFLV.download(vidURL);
		}
		private function onFLVDownload(evt:PHPTubeEvent):void {
			decoder = new FLVtoRaw(ByteArray(evt.data));
			var player:MP3Pitch	= new MP3Pitch(decoder);
			trace("stream starting...");
			player.start();
		}
	}
}
internal class SingletonBlocker {}
